package com.global.model;

import java.io.Serializable;
import javax.persistence.*;

import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;


/**
 * The persistent class for the ZLY_ST_BSC_GDCL database table.
 * @author zly601436
 */
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate=true)
@Table(name = "ZLY_ST_BSC_GDCL", uniqueConstraints = {
		@UniqueConstraint(columnNames = { "GRID_ID", "LABEL" }),
		@UniqueConstraint(columnNames = { "GRID_ID", "DSO_COLUMN_ID" })})
@SequenceGenerator(name="ZLY_ST_BSC_GDCL_ID_GENERATOR", sequenceName="ZLY_ST_BSC_GDCL_ID_SEQ", initialValue=1000, allocationSize=1)	
@NamedQueries({
        @NamedQuery(
                name = "findGridColumnByGridAndLabel",
                query = "select g from GridColumn g where g.grid.id = :gridId and g.label = :label "
        ),
        @NamedQuery(
                name = "findGridColumnByGridAndDsod",
                query = "select g from GridColumn g where g.grid.id = :gridId and g.dataSourceXColumn.id = :dataSourceXColumnId "
        ),
        @NamedQuery(
                name = "findGridColumnsByDsod",
                query = "select g from GridColumn g where g.dataSourceXColumn.id = :dataSourceXColumnId "
        ),
        @NamedQuery(
                name = "findGridColumnsByGrid",
                query = "select g from GridColumn g where g.grid.id = :gridId "
        ),
        @NamedQuery(
                name = "findDisplayGridColumnsByGrid",
                query = "select g from GridColumn g where g.grid.id = :gridId and g.displayIndex>0 "
        )       
})
public class GridColumn extends BaseObject implements Serializable {

	private static final long serialVersionUID = -3772667354136446898L;
	
	private Long id;
	private String label;
	private int displayIndex;
	private int displayMaxWidth;
	
	private DataSourceXColumn dataSourceXColumn;
	private Grid grid;

    /**
     * 定义操作人、操作时间
     */
    private Who who;

    public GridColumn() {
    }

    public GridColumn(Grid grid) {
    	this.grid = grid;
    }
    
	@Id	
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ZLY_ST_BSC_GDCL_ID_GENERATOR")
	@SearchableId
	public Long getId() {
		return this.id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	/**
	 * 返回Grid栏位的显示名称
	 * @return String
	 */
	@Column(name="LABEL", nullable=false, length=512)
	@SearchableProperty
	public String getLabel() {
		return this.label;
	}
	/**
	 * 设置Grid栏位的显示名称
	 * @param displayName
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * 返回Grid栏位的显示顺序
	 * @return int
	 */
	@Column(name="DISPLAY_INDEX", nullable=false, precision=22)
	public int getDisplayIndex() {
		return this.displayIndex;
	}
	/**
	 * 设置Grid栏位的显示顺序
	 * @param displayIndex 必须大于等于0
	 */
	public void setDisplayIndex(int displayIndex) {
		this.displayIndex = displayIndex<0 ? 0 : displayIndex;
	}
	
	/**
	 * true表示当前GridColumn为显示栏位
	 * @return boolean
	 */
	@Transient
	public boolean isDisplay(){
		return displayIndex>0 ? true : false;
	}

	/**
	 * 返回Grid栏位的最大显示宽度
	 * @return int
	 */
	@Column(name="DISPLAY_MAX_WIDTH", nullable=false)
	public int getDisplayMaxWidth() {
		return displayMaxWidth;
	}
	/**
	 * 设置Grid栏位的最大显示宽度
	 * @param displayMaxWidth
	 */
	public void setDisplayMaxWidth(int displayMaxWidth) {
		this.displayMaxWidth = (displayMaxWidth<0 ? 0 : displayMaxWidth);
	}
	
	//bi-directional many-to-one association to DataSourceXColumn
    @ManyToOne
	@JoinColumn(name="DSO_COLUMN_ID", nullable=false)
	public DataSourceXColumn getDataSourceXColumn() {
		return this.dataSourceXColumn;
	}
	public void setDataSourceXColumn(DataSourceXColumn dataSourceXColumn) {
		this.dataSourceXColumn = dataSourceXColumn;
	}	

	//bi-directional many-to-one association to Grid
    @ManyToOne
	@JoinColumn(name="GRID_ID", nullable=false)
	public Grid getGrid() {
		return this.grid;
	}
	public void setGrid(Grid grid) {
		this.grid = grid;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public Who getWho() {
		return who;
	}
	/**
     * {@inheritDoc}
     */
	@Override
	public void setWho(Who who) {
		this.who = who;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((label == null) ? 0 : label.hashCode());
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GridColumn other = (GridColumn) obj;
		if (label == null) {
			if (other.label != null)
				return false;
		} else if (!label.equals(other.label))
			return false;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "GridColumn [label=" + label + ", grid=" + grid.getDescription() + "]";
	}
}